<?php

namespace App\Jobs;

use App\Models\Mysql\XnMerchantGoodsInvoicing;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class MerchantGoodsTimePrice implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    private $data;
//    public $tries = 3;
    public function __construct($data = [])
    {
        $this->data = $data;

    }
    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $key = $this->data['business_number'].$this->data['merchant_num'].'MerchantGoodsTimePrice'.$this->data['id'];
        $redis = new \redis();
        $redis->connect('127.0.0.1', 6379);//连接redis
        $info =$redis->get($key);
        if (empty($info)){
            echo  '修改已被撤销';
        }else{
            $where = [
                'goods_id'=>$this->data['id'],
                'merchant_num'=>$this->data['merchant_num']
            ];
            $array = [
                'merchant_num'=>$this->data['merchant_num'],
                'goods_id'=>$this->data['id'],
                'price'=>$this->data['price'],//销售价
            ];
            $goods = DB::connection($this->data['db'])->table('xn_merchant_goods_invoicing')->where($where)->first();
            if (empty($goods)){
                $res = DB::connection($this->data['db'])->table('xn_merchant_goods_invoicing')->where($where)->insert($array);
            }else{
                $res = DB::connection($this->data['db'])->table('xn_merchant_goods_invoicing')->where($where)->update($array);
            }
            if ($res){
                $redis->del($key);
            }
        }
    }
}
